<?php

namespace Common\Model;

/**
 * 子管理员-组关系表
 * @modify trainyao 2016-07-26  创建
 * @index isdelete_groupInfoId(`isdelete`, `groupinfo_id`)
 */
Class GroupAdminModel extends \Common\Model\BaseModel {

    protected $trueTableName = 'tb_group_admin';
    protected $field = 'is,groupinfo_id,adminlogin_id,isdelete';

    /**
     * 根据组ID获取组-子管理员关系
     * @modify trainyao 2016-07-26 创建
     * @param  mixin   $groupId   组ID(s)
     * @return array              组-子管理员关系信息数组
     */
    public function getInfoByGroup($groupId) {
        if (!is_array($groupId)) {
            $groupId = explode(',', $groupId);
        }

        $option = array(
            'isdelete' => NOT_DELETE,
            'groupinfo_id' => count($groupId) == 1 ? $groupId[0] : array('in', $groupId),
        );
        $data = $this->where($option)->select();

        return empty($data) ? array() : $data;
    }

    /**
     * 增加组-子管理员关系
     * @modify trainyao 2016-07-26 创建
     * @return boolean   执行结果
     */
    public function addInfoByGroup($groupId, $adminLoginId) {
        $userId = getUserId();
        $data = array();
        foreach ($adminLoginId as $adminLoginIdItem) {
            array_push($data, array(
                'adminlogin_id' => $adminLoginIdItem,
                'groupinfo_id' => $groupId,
                'creator' => $userId,
                'operator' => $userId,
            ));
        }
        unset($adminLoginIdItem);

        return $this->addAll($data) ? true : false;
    }

    /**
     * 删除特定组和子管理员ID的关系记录
     * @modify trainyao 2016-07-26    创建
     * @param mixin  $groupId    组ID(s)  
     * @param mixin  $adminLoginId   子管理员ID  
     * @return boolean   执行结果
     */
    public function deleteInfoByGroupAdmin($groupId, $adminLoginId = null) {
        if (!is_array($groupId)) {
            $groupId = explode(',', $groupId);
        }

        $option = array(
            'isdelete' => NOT_DELETE,
            'groupinfo_id' => count($groupId) == 1 ? $groupId[0] : array('in', $groupId),
        );
        if (!is_null($adminLoginId)) {
            if (!is_array($adminLoginId)) {
                $adminLoginId = explode(',', $adminLoginId);
            }
            $option['adminlogin_id'] = count($adminLoginId) == 1 ? $adminLoginId[0] : array('in', $adminLoginId);
        }
        $data = array(
            'isdelete' => IS_DELETE,
        );

        return $this->where($option)->save($data) === false ? false : true;
    }
}
